/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.aileron.web.text;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

import cc.aileron.commons.accessor.ConvertorNotFoundException;
import cc.aileron.commons.exception.ModuleConfigurationException;
import cc.aileron.commons.resource.ResourceNotFoundException;
import cc.aileron.commons.utility.ResourceUtils;
import cc.aileron.dao.db.DbConnectionManagerSimple;
import cc.aileron.dao.db.DbDaoModule;
import cc.aileron.dao.db.logger.NullLogger;
import cc.aileron.dao.db.logger.StdoutLogger;
import cc.aileron.template.sql.impl.SqlLogger;
import cc.aileron.web.text.dao.TextDaoConduct;
import cc.aileron.wsgi.WsgiContextDestroyer;
import cc.aileron.wsgi.WsgiDevelopmentMode;

import com.google.inject.Binder;
import com.google.inject.Module;

/**
 * application-module
 * @author Aileron
 *
 */
public class TextModule implements Module,WsgiContextDestroyer,WsgiDevelopmentMode
{
    /**
     * SETTING
     */
    private static final String SETTING_FILE_PATH = "/opt/setting/aileron-framework-web-text.properties";
    
    @Override
    public void configure(final Binder binder)
    {
        binder.install(db);
    }
    
    @Override
    public boolean isDevelopment()
    {
        return this.isDebug;
    }
    
    @Override
    public void destroy() throws Exception
    {
        db.connectionManager.destroy();
    }
    
    /**
     * construct
     * @throws ModuleConfigurationException 
     * @throws ResourceNotFoundException 
     * @throws IOException 
     * @throws SQLException 
     * @throws ConvertorNotFoundException 
     * @throws ClassNotFoundException 
     */
    public TextModule() 
    throws ModuleConfigurationException, IOException, ResourceNotFoundException, ClassNotFoundException, ConvertorNotFoundException, SQLException
    {
        final Properties properties = ResourceUtils.resource(SETTING_FILE_PATH).toProperties();
        isDebug = properties.getProperty("template.debug").equals("true");
        
        final SqlLogger sqlLogger = Boolean.parseBoolean(properties.getProperty("db.debug"))
        ? new StdoutLogger()
        : new NullLogger();
        {
            final DbConnectionManagerSimple connectionManagerSimple = 
                new DbConnectionManagerSimple( properties );
            this.db = new DbDaoModule(new TextDaoConduct(connectionManagerSimple,sqlLogger));
        }
    }
    
    /**
     * is-debug
     */
    private final boolean isDebug;
    
    /**
     * db-module
     */
    private final DbDaoModule db ;
}